﻿#include <QCoreApplication>

#include <assert.h>
#include <QAudioFormat>
#include <QAudioOutput>
#include <QThread>

//装备好pcm数据
//ffmpeg -i test.mp4 -f s16le out.pcm


int main(int argc, char *argv[])
{
    FILE * fp  = fopen("out.pcm","rb");

    if(NULL == fp)
    {
        assert(false);
    }

    QAudioFormat format;

    format.setSampleRate(44100);
    format.setSampleSize(16);
    format.setChannelCount(2);
    format.setCodec("audio/pcm");

    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::UnSignedInt);

    QAudioOutput * out = new QAudioOutput(format);

    QIODevice * io = out->start(); //开始播放

    int size = out->periodSize();  //一个周期需要播放的大小

    char * buffer = new  char [size];

    while(!feof(fp))
    {
        if(out->bytesFree() < size)
        {
            QThread::msleep(1);
            continue;
        }
        int readlen = fread(buffer, 1, size, fp);

        if(readlen <= 0) break;

        io->write(buffer, readlen);

    }

    fclose(fp);
    delete [] buffer;
    buffer = NULL;

    out->stop();

    delete out;


    getchar();
    return 0;
}
